﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace WP
{
    public static class DataAccess
    {
        public static string wp_GetScript(int id)
        {
            using (SqlConnection conn = Common.GetConnection())
            {
                conn.Open();
                using (SqlCommand cmd = Common.InitCmd("wp_GetScript", conn, CommandType.StoredProcedure))
                {
                    cmd.Parameters.AddWithValue("@state", id);

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        rdr.Read();
                        //todo kezelni kell a NULL esetet!

                        return rdr.GetString(0);
                    }
                }
            }
        }

        public static int wp_GetCommonState(int sourcestate, int targetstate)
        {
            using (SqlConnection conn = Common.GetConnection())
            {
                conn.Open();
                using (SqlCommand cmd = Common.InitCmd("wp_GetCommonState", conn, CommandType.StoredProcedure))
                {
                    cmd.Parameters.AddWithValue("@sourceState", sourcestate);
                    cmd.Parameters.AddWithValue("@targetState", targetstate);

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        rdr.Read();
                        return (int)rdr[0];
                    }
                }
            }
        }

        public static int wp_GetStateObjectID(int state)
        {
            using (SqlConnection conn = Common.GetConnection())
            {
                conn.Open();
                using (SqlCommand cmd = Common.InitCmd("wp_GetStateObjectID", conn, CommandType.StoredProcedure))
                {
                    cmd.Parameters.AddWithValue("@State", state);

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        rdr.Read();
                        return (int)rdr[0];
                    }
                }
            }
        }

        public static int wp_LatestVersion(int objectid, int state)
        {
            using (SqlConnection conn = Common.GetConnection())
            {
                conn.Open();
                using (SqlCommand cmd = Common.InitCmd("wp_LatestVersion", conn, CommandType.StoredProcedure))
                {
                    cmd.Parameters.AddWithValue("@ObjectID ", objectid);
                    cmd.Parameters.AddWithValue("@State  ", state);

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        rdr.Read();
                        return (int)rdr[0];
                    }
                }
            }
        }

        public static List<State> wp_GetParents(int currentState, int parentState)
        {
            List<State> returnvalue = new List<State>();
            using (SqlConnection conn = WP.Common.GetConnection())
            {
                conn.Open();
                using (SqlCommand cmd = Common.InitCmd("wp_GetParents", conn, CommandType.StoredProcedure))
                {
                    cmd.Parameters.AddWithValue("@currentState", currentState);
                    cmd.Parameters.AddWithValue("@parentState", parentState);

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            returnvalue.Add(rdr.GetInt32(0));
                        }
                    }
                }
            }
            return returnvalue;
        }
    }
}
